import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/jsonserialize/serve/data.dart';
import 'package:im_flutter_sdk_example/pages/serve/widgets/serve_item.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

class ServeList extends StatefulWidget {
  final isRecommend;
  const ServeList({Key key, this.isRecommend}) : super(key: key);
  @override
  _ActiveListState createState() => _ActiveListState();
}

class _ActiveListState extends State<ServeList>
    with AutomaticKeepAliveClientMixin {
  List<ServeData> serveData = [];
  @override
  void initState() {
    super.initState();
    this.getData();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Container(
        color: rgba(255, 255, 255, 1),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.symmetric(horizontal: 16),
              child: Column(
                children: [
                  Column(
                    children: widget.isRecommend == true && serveData.length > 2
                        ? serveData
                            .getRange(0, 3)
                            .map((item) => ServeItem(item: item))
                            .toList()
                        : serveData
                            .map((item) => ServeItem(item: item))
                            .toList(),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

  void getData() async {
    G.showLoading(status: '加载中...');
    var res = await G.req.home.serviceList();
    res.data['data']['list'].forEach((e) {
      ServeData list = ServeData.fromJson(e);
      serveData.add(list);
    });
    this.setState(() {});
    G.dismissLoading();
  }
}
